package me.ivps.android.weibo.dao.shorturl;

import me.ivps.android.weibo.bean.MessageBean;
import me.ivps.android.weibo.bean.ShareListBean;
import me.ivps.android.weibo.dao.URLHelper;
import me.ivps.android.weibo.support.debug.AppLogger;
import me.ivps.android.weibo.support.error.WeiboException;
import me.ivps.android.weibo.support.http.HttpMethod;
import me.ivps.android.weibo.support.http.HttpUtility;
import me.ivps.android.weibo.support.settinghelper.SettingUtility;
import me.ivps.android.weibo.support.utils.TimeUtility;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

/**
 * 获取包含指定单个短链接的最新微博内容
 * 
 * User: qii Date: 13-2-27
 */
public class ShareShortUrlTimeLineDao {
    
    private String getMsgListJson() throws WeiboException {
        String url = URLHelper.SHORT_URL_SHARE_TIMELINE;
        
        Map<String, String> map = new HashMap<String, String>();
        map.put("access_token", access_token);
        map.put("count", count);
        map.put("max_id", max_id);
        map.put("url_short", url_short);
        
        String jsonData = null;
        
        jsonData = HttpUtility.getInstance().executeNormalTask(HttpMethod.Get,
                url, map);
        
        return jsonData;
    }
    
    public ShareListBean getGSONMsgList() throws WeiboException {
        
        String json = getMsgListJson();
        Gson gson = new Gson();
        
        ShareListBean value = null;
        try {
            value = gson.fromJson(json, ShareListBean.class);
        }
        catch (JsonSyntaxException e) {
            
            AppLogger.e(e.getMessage());
        }
        
        if (value != null) {
            
            Iterator<MessageBean> iterator = value.getItemList().iterator();
            
            while (iterator.hasNext()) {
                
                MessageBean msg = iterator.next();
                if (msg.getUser() == null) {
                    iterator.remove();
                }
                else {
                    msg.getListViewSpannableString();
                    TimeUtility.dealMills(msg);
                }
            }
            
        }
        
        return value;
    }
    
    private String access_token;
    private String url_short;
    private String count;
    private String max_id;
    
    public ShareShortUrlTimeLineDao(String access_token, String url_short) {
        
        this.access_token = access_token;
        this.url_short = url_short;
        this.count = SettingUtility.getMsgCount();
    }
    
    public ShareShortUrlTimeLineDao setCount(String count) {
        this.count = count;
        return this;
    }
    
    public ShareShortUrlTimeLineDao setMaxId(String max_id) {
        this.max_id = max_id;
        return this;
    }
}
